package com.fr.chicken.controller;

import com.fr.chicken.dao.GoodsDao;
import com.fr.chicken.dao.RechargeLogDao;
import com.fr.chicken.manager.configure.ConfigManager;
import com.fr.chicken.manager.configure.EConfigure;
import com.fr.chicken.manager.playerinfo.PlayerInfoManager;
import com.fr.chicken.model.Goods;
import com.fr.chicken.model.PlayerInfo;
import com.fr.chicken.model.RechargeLog;
import com.fr.chicken.pay.com.shb.pay.main.Payment;
import com.fr.chicken.pay.com.shb.pay.main.QuickPayment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/pay")
public class Pay {
    @Autowired
    private Payment payment;

    @Autowired
    private GoodsDao goodsDao;

    @Autowired
    private RechargeLogDao rechargeLogDao;
    @Autowired
    private PlayerInfoManager playerInfoManager;

    @Autowired
    private ConfigManager configManager;

    @RequestMapping(value = "/shb", method = {RequestMethod.GET})
    @ResponseBody
    public String shbNotify(Integer shopId,String  defrayalType,Long playerId) {
        Goods goods = goodsDao.selectByPrimaryKey(shopId);
        if(goods == null){
            return "";
        }
        try {
            double price = goods.getPrice();
            PlayerInfo playerInfo = playerInfoManager.findPlayerInfoById(playerId);
           if(playerInfo != null && playerInfo.getAgent() == 1){
               price = price * (configManager.getIntConfig(EConfigure.AgentPrice) / (double)100);;
           }
             return payment.payment(playerId,shopId,payment.buildBusinessContextFormPay(shopId,defrayalType,goods.getName(),goods.getName(),price,"8.8.8.8")).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    @RequestMapping(value = "/queryOrder", method = {RequestMethod.GET})
    @ResponseBody
    public int queryOrder(String orderNumber) {
        RechargeLog log = rechargeLogDao.selectRechargeLogByOrderNum(orderNumber);
        if(log == null){
            return 0;
        }

        return log.getStat();
    }

}
